define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'cityarea/cityarea/index',
                    add_url: 'cityarea/cityarea/add',
                    edit_url: 'cityarea/cityarea/edit',
                    multi_url: 'cityarea/cityarea/multi',
                    del_url: 'cityarea/cityarea/del',
                    table: 'fa_cityarea',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                escape: false,
                pk: 'id',
                sortName: 'weigh', // 修改为按weigh字段排序
                sortOrder: 'asc', // 默认降序排列
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: '名称'},
                        {field: 'status', title: '状态', operate: false, formatter:Controller.api.formatter.status},
                        {field: 'weigh', title: __('Weigh'), visible: false}, // 添加weigh字段但不显示
                        {field: 'operate', title: __('Operate'), table: table,
                            buttons: [
                                {
                                    name: 'dragsort',
                                    icon: 'fa fa-arrows',
                                    title: __('Drag to sort'),
                                    extend: 'data-toggle="tooltip"',
                                    classname: 'btn btn-xs btn-primary btn-dragsort',
                                }
                            ],
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
                // 启用拖动排序
                onReorderRows: function(table, rows) {
                    var ids = $.map(rows, function(row) {
                        return row.id;
                    });
                    // 发送排序请求
                    $.post('ajax/weigh', {
                        ids: ids.join(','),
                        table: 'fa_cityarea',
                        field: 'weigh',
                        orderway: 'desc'
                    }, function(data) {
                        Toastr.success('排序成功');
                    });
                }
            });
            var script = document.createElement('script');
            script.src = '/assets/js/backend/common/add.js?';
            document.head.appendChild(script);
            // 为表格绑定事件
            Table.api.bindevent(table);

            // 启用拖动排序
            table.bootstrapTable('enableReorder');
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                url: function (value, row, index) {
                    return '<div class="input-group input-group-sm" style="width:250px;"><input type="text" class="form-control input-sm" value="' + value + '"><span class="input-group-btn input-group-sm"><a href="' + value + '" target="_blank" class="btn btn-default btn-sm"><i class="fa fa-link"></i></a></span></div>';
                },
                ip: function (value, row, index) {
                    return '<a class="btn btn-xs btn-ip bg-success"><i class="fa fa-map-marker"></i> ' + value + '</a>';
                },
                changeStatus: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return '<a class="btn-change text-success" data-url="cityarea/cityarea/changeStatus" data-confirm="确认切换状态？" data-id="' + row.id + '"><i class="fa ' + (row.title == '' ? 'fa-toggle-on fa-flip-horizontal text-gray' : 'fa-toggle-on') + ' fa-2x">aabb</i></a>';
                },
                status: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return '<a class="btn-change text-success" data-url="cityarea/cityarea/status" data-confirm="确认切换状态？" data-id="' + row.id + '"><i class="fa ' + (row.status == 0 ||row.status == null ? 'fa-toggle-on fa-flip-horizontal text-gray' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                },
            },
            events: {//绑定事件的方法
                ip: {
                    //格式为：方法名+空格+DOM元素
                    'click .btn-ip': function (e, value, row, index) {
                        e.stopPropagation();
                        var container = $("#table").data("bootstrap.table").$container;
                        var options = $("#table").bootstrapTable('getOptions');
                        //这里我们手动将数据填充到表单然后提交
                        $("form.form-commonsearch [name='ip']", container).val(value);
                        $("form.form-commonsearch", container).trigger('submit');
                        Toastr.info("执行了自定义搜索操作");
                    }
                },
            }
        }
    };
    return Controller;
});